package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.wx.goods.vo.UserIndexVo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.wx.WxOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname WxOrdersServiceImpl
 * @Description
 * @Date 2021/8/14 17:46
 * @Author : xujun
 */
@Service
@Transactional
public class WxOrdersServiceImpl implements WxOrdersService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public UserIndexVo index(String username) {

        //  未登录
        if (username == null) {
            return new UserIndexVo(new UserIndexVo.UserIndexInnerVo(0, 0, 0, 0));
        }

        // 通过用户名获得用户对象
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria7 = userExample.createCriteria();
        criteria7.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        User user = users.get(0);

        // 未付款 101
        OrderExample unpaidExample = new OrderExample();
        OrderExample.Criteria criteria1 = unpaidExample.createCriteria();
        criteria1.andOrderStatusEqualTo((short) 101).andUserIdEqualTo(user.getId());
        List<Order> unpaid = orderMapper.selectByExample(unpaidExample);

        // 已发货 301
        OrderExample deliveredExample = new OrderExample();
        OrderExample.Criteria criteria2 = deliveredExample.createCriteria();
        criteria2.andOrderStatusEqualTo((short) 301).andUserIdEqualTo(user.getId());
        List<Order> delivered = orderMapper.selectByExample(deliveredExample);

        // 用户收货 401
        OrderExample receivedExample = new OrderExample();
        OrderExample.Criteria criteria5 = receivedExample.createCriteria();
        criteria5.andOrderStatusEqualTo((short) 401).andUserIdEqualTo(user.getId());
        List<Order> received = orderMapper.selectByExample(receivedExample);

        // 已付款 201
        OrderExample paidExample = new OrderExample();
        OrderExample.Criteria criteria3 = paidExample.createCriteria();
        criteria3.andOrderStatusEqualTo((short) 201).andUserIdEqualTo(user.getId());
        List<Order> paid = orderMapper.selectByExample(paidExample);

        // 未评论 查表
//        OrderExample unCommentExample= new OrderExample();
//        unCommentExample.createCriteria().andUserIdEqualTo(user.getId()).andCommentsGreaterThan((short) 0);
//        List<Order> unComment = orderMapper.selectByExample(unCommentExample);
//        int unCommentCount = 0;
//        for (Order order : unComment) {
//            unCommentCount += order.getComments();
//        }

        // 用户收货 401
        OrderExample unCommentExample1= new OrderExample();
//        unCommentExample1.createCriteria()
//                .andOrderStatusEqualTo((short) 401)
//                .andDeletedEqualTo(false);
//        List<Order> unComment1 = orderMapper.selectByExample(unCommentExample1);
//        // 系统收货 402
//        OrderExample unCommentExample2= new OrderExample();
//        unCommentExample2.createCriteria()
//                .andOrderStatusEqualTo((short) 402)
//                .andDeletedEqualTo(false);
//        List<Order> unComment2 = orderMapper.selectByExample(unCommentExample2);
        OrderExample.Criteria criteria = unCommentExample1.createCriteria();
        criteria.andOrderStatusBetween(new Short("400"), new Short("500")).andCommentsGreaterThan(new Short("0")).andDeletedEqualTo(false).andUserIdEqualTo(user.getId());;
        List<Order> unComment1 = orderMapper.selectByExample(unCommentExample1);
        // 401 + 402
//        int unComment = unComment1.size() + unComment2.size();
        int unComment = unComment1.size();


        // 封装返回
        UserIndexVo userIndexVo = new UserIndexVo();
        UserIndexVo.UserIndexInnerVo vo = new UserIndexVo.UserIndexInnerVo();
        // 未付款 101
        vo.setUnpaid(unpaid.size());

//        // 未收货 = 已发货- 用户收货    301-401
//        vo.setUnrecv(received.size() - delivered.size());
        // 冯香洁逻辑：(用户)未收货 = (商家)已发货
        vo.setUnrecv(delivered.size());

//        // 未发货 = 已付款 - 已发货     201-301
//        vo.setUnship(paid.size() - delivered.size());
        // 冯香洁逻辑：(商家)未发货 = (用户)已付款
        vo.setUnship(paid.size());

        // 未评论
//        vo.setUncomment(unCommentCount);
        // 冯香洁逻辑：未评论 = 系统收货 + 用户收货
        vo.setUncomment(unComment);

        userIndexVo.setOrder(vo);

        return userIndexVo;
    }
}
